#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL fa[maxn],flag[maxn];
LL find(LL x){
    if(fa[x]!=x) return fa[x]=find(fa[x]);
    return fa[x];
}
struct P{
    LL x,y,val;
}p[maxn];
bool cmp(P A,P B){
    return A.val>B.val;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++) fa[i]=i;
  for(LL i=1;i<=m;i++){
      LL num;cin>>num;
      if(num==1) cin>>p[i].x,p[i].y=p[i].x,cin>>p[i].val;
      if(num==2) cin>>p[i].x>>p[i].y>>p[i].val;
  }
  sort(p+1,p+1+m,cmp);
  LL ans=0;
  for(LL i=1;i<=m;i++){
    LL x=find(p[i].x);LL y=find(p[i].y);
    if(x==y){
        if(flag[x]) continue;
        else{
            ans+=p[i].val;
            flag[x]=1;
        }
    }
    else{
        if(flag[x]&&flag[y]) continue;
        else if(!flag[x]&&!flag[y]){
            ans+=p[i].val;
            fa[x]=y;
        }
        else{
            ans+=p[i].val;
            fa[x]=y;
            flag[y]=1;
        }
    }
  }
  cout<<ans<<"\n";
return 0;
}
